﻿using System;
using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace ZhukBlogSharePointProject.CustomEntityPicker
{
    /// <summary>
    /// Наше кастомное lookup поле (тип, который по-русски зовется "Подстановка")
    /// </summary>
    public class CustomSPFieldLookup : SPFieldLookup
    {
        public CustomSPFieldLookup(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
            Type = SPFieldType.Lookup;
            TypeAsString = "CustomSPFieldLookup";
        }

        public CustomSPFieldLookup(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
            Type = SPFieldType.Lookup;
            TypeAsString = "CustomSPFieldLookup";
        }

        public override void OnAdded(SPAddFieldOptions op)
        {
            base.OnAdded(op);
            if (ParentList == null) return;
            if (ParentList.ParentWeb == null) return;
            var siteId = ParentList.ParentWeb.Site.ID;
            var webId = ParentList.ParentWeb.ID;
            SPSecurity.RunWithElevatedPrivileges(
                () =>
                {
                    using (var site = new SPSite(siteId))
                    {
                        using (var web = site.OpenWeb(webId))
                        {
                            var customList = web.Lists["CustomList"];
                            LookupWebId = web.ID;
                            LookupList = customList.ID.ToString();
                            LookupField = "Title";
                            Update();
                        }
                    }
                });
        }

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl control;
                if (CountRelated)
                {
                    control = new ComputedField();
                }
                else if (AllowMultipleValues)
                {
                    control = new CustomMultipleLookupField();

                }
                else
                {
                    control = new CustomLookupField();
                }
                control.FieldName = InternalName;
                return control;
            }
        }

        public override object GetFieldValue(string value)
        {
            return base.GetFieldValue(value);
        }

        public override string GetFieldValueAsHtml(object value)
        {
            return base.GetFieldValueAsHtml(value);
        }

        public override string GetFieldValueAsText(object value)
        {
            return base.GetFieldValueAsText(value);
        }

        public override string GetFieldValueForEdit(object value)
        {
            return base.GetFieldValueForEdit(value);
        }

        public override void Update()
        {
            if (GetCustomProperty("AllowMultiple") != null)
            {
                AllowMultipleValues = (bool)GetCustomProperty("AllowMultiple");
            }
            base.Update();
        }

        public void SetCustomAttr(string propertyName, object propertyValue)
        {
            var type = typeof(SPField);
            if (propertyValue != null)
            {
                var set = type.GetMethod("SetFieldAttributeValue", BindingFlags.NonPublic | BindingFlags.Instance);
                set.Invoke(this, new object[] { propertyName, propertyValue.ToString() });
            }
            else
            {
                var remove = type.GetMethod("RemoveFieldAttributeValue", BindingFlags.NonPublic | BindingFlags.Instance);
                remove.Invoke(this, new object[] { propertyName });
            }
        }

        public string GetCustomAttr(string propertyName)
        {
            var type = typeof(SPField);
            var getField = type.GetMethod("GetFieldAttributeValue", BindingFlags.NonPublic | BindingFlags.Instance, null, new [] { typeof(String) }, null);
            var o = getField.Invoke(this, new object[] { propertyName });
            return o as String;
        }

        public override bool AllowMultipleValues
        {
            get
            {
                return base.AllowMultipleValues;
            }
            set
            {
                base.AllowMultipleValues = value;
                this.SetAttribute("Type", "FieldContractorField");
            }
        }
    }
}
